Public
Edited
Mar 21, 2023
1 fork
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
output = {
// Grab just the selected column of data
const column = data.map((d) => d[select]);

// Initialise an empty string
let outstr = "";

// Calculate max and min of the column, filtering out NaN values
// Edit these to hardcode the domain of your data
const datamin = Math.min(...column.filter((d) => !isNaN(d)));
const datamax = Math.max(...column.filter((d) => !isNaN(d)));

// Loop over the data
for (let datapoint of column) {
//
// Check if datapoint is not a number
if (isNaN(datapoint)) {
//
// In this case, skip the assignment with a hyphen
outstr = outstr.concat("-, ");

// If it is a number
} else {
//
// Calculate matching element number in the mapping array
const outnumber = Math.round(
scale(+datapoint, datamin, datamax, 0, mapto.length - 1)
);

// Push the matched element to the output string
outstr = outstr.concat(mapto[outnumber], ", ");
}
}

// Remove the last ", "
return outstr.substring(0, outstr.length - 2);
}
Insert cell
// This function scales values from one range to another
function scale(value, inMin, inMax, outMin, outMax) {
const result =
((value - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin;

if (result < outMin) {
return outMin;
} else if (result > outMax) {
return outMax;
}

return result;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more